/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package VMCategoriesSettings;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import GuiTools.*;
import Settings.Language;

/**
 * @author Christoph Schnack
 *
 */
class CategoryListPanel extends JPanel{
	private DefaultListModel dlm;
	private JList list;
	private EditButton delBtn;
	JButton upBtn, downBtn;
	private MDTribuitFrame frame;
	private Language LANG;
	
	public CategoryListPanel(MDTribuitFrame frameArg, Language langArg, DefaultListModel dlmArg,String title){
		super();
		frame = frameArg;
		LANG = langArg;
		dlm = dlmArg;
		list = new JList(dlm);		
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		CustomScrollPane csp = new CustomScrollPane(list);
		csp.setPreferredSize(new Dimension(150,250));
		EditButton addBtn = new EditButton("+",true);
		addBtn.addActionListener(new AddEntryListener());
		delBtn = new EditButton("-",true);
		delBtn.addActionListener(new DelEntryListener());
		upBtn = new JButton("^");
		upBtn.addActionListener(new UpBtnListener());
		downBtn = new JButton("v");
		downBtn.addActionListener(new DownBtnListener());
		JPanel listBtnPanel = new JPanel();
		listBtnPanel.add(addBtn);
		listBtnPanel.add(delBtn);
		listBtnPanel.add(upBtn);
		listBtnPanel.add(downBtn);
		JLabel titleLabel = new JLabel(title);
		titleLabel.setHorizontalAlignment(JLabel.CENTER);
		titleLabel.setPreferredSize(new Dimension(150,titleLabel.getPreferredSize().height));
		titleLabel.setFont(new Font(Font.SANS_SERIF,Font.BOLD,15));
		
		setButtonsActivated(dlm.size()>0);
		
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		this.add(titleLabel);
		this.add(csp);
		this.add(listBtnPanel);
	}
	
	public DefaultListModel getDLM(){
		return dlm;
	}
	
	public void setButtonsActivated(boolean arg){
		delBtn.setEnabled(arg);
		if(dlm.size()==1){
			upBtn.setEnabled(false);
			downBtn.setEnabled(false);
		}
		else{
			upBtn.setEnabled(arg);
			downBtn.setEnabled(arg);
		}
	}
	
	private class UpBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(list.getSelectedValue()!=null){
				if(list.getSelectedIndex()>0){
					String oldReminder = (String)list.getSelectedValue();
					dlm.setElementAt(dlm.getElementAt(list.getSelectedIndex()-1), list.getSelectedIndex());
					dlm.setElementAt(oldReminder, list.getSelectedIndex()-1);
					list.setSelectedIndex(list.getSelectedIndex()-1);
				}
			}
			else{
				JOptionPane.showMessageDialog(frame,LANG.getNothingChosen(),LANG.getAttention(),JOptionPane.WARNING_MESSAGE);
			}
		}
	}
	
	private class DownBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(list.getSelectedValue()!=null){
				if(list.getSelectedIndex()<dlm.size()-1){
					String oldReminder = (String)list.getSelectedValue();
					dlm.setElementAt(dlm.getElementAt(list.getSelectedIndex()+1), list.getSelectedIndex());
					dlm.setElementAt(oldReminder, list.getSelectedIndex()+1);
					list.setSelectedIndex(list.getSelectedIndex()+1);
				}
			}
			else{
				JOptionPane.showMessageDialog(frame,LANG.getNothingChosen(),LANG.getAttention(),JOptionPane.WARNING_MESSAGE);
			}
		}
	}
	
	private class AddEntryListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String input = JOptionPane.showInputDialog(frame, LANG.getNewCategoryName());
			if(input==null)return;
			input = input.trim();
			if(input.equals("") || !GuiTools.checkInput(input)){
				GuiTools.showEmptyOrInvalidInputWarning(LANG, frame, LANG.getNewCategoryName());
				return;
			}
			if(input.equals("n.a.")){
				JOptionPane.showMessageDialog(frame, LANG.getNAIsAReservedWord(), LANG.getAttention(), JOptionPane.WARNING_MESSAGE);
				return;
			}
			if(!dlm.contains(input)){
				dlm.addElement(input);
				setButtonsActivated(true);
			}
			else{
				JOptionPane.showMessageDialog(frame, LANG.getCategoryExistsAlready(), LANG.getAttention(), JOptionPane.WARNING_MESSAGE);
			}
		}
	}
	
	private class DelEntryListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(list.getSelectedValue()!=null){
				dlm.removeElement(list.getSelectedValue());
				setButtonsActivated(dlm.getSize()>0);
			}
			else{
				JOptionPane.showMessageDialog(frame,LANG.getNothingChosen(),LANG.getAttention(),JOptionPane.WARNING_MESSAGE);
			}
		}
	}
}
